﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Demo.DBContext;
using Demo.DtoParameters;
using Demo.Entities;
using Demo.Helpers;
using Microsoft.EntityFrameworkCore;

namespace Demo.Repository
{
    public class EmployeeRepository : IEmployeeRepository
    {
        private readonly RoutineDbContext _context;

        public EmployeeRepository(RoutineDbContext context)
        {
            _context = context ?? throw new ArgumentNullException(nameof(context));
        }

        public async Task<PageList<Employee>> GetEmployeesAsync(Guid companyId, EmployeeDtoParameters parameters)
        {
            if (companyId.Equals(Guid.Empty))
            {
                throw new ArgumentNullException(nameof(companyId));
            }

            var item = _context.Employees.Where(et => et.CompanyId == companyId);

            if (!string.IsNullOrWhiteSpace(parameters.Gender))
            {
                item = item.Where(et => et.Gender == Enum.Parse<Gender>(parameters.Gender));
            }

            if (!string.IsNullOrWhiteSpace(parameters.Q))
            {
                item = item.Where(
                    et => et.FirstName.Contains(parameters.Q)
                          || et.LastName.Contains(parameters.Q)
                          || et.EmployeeNo.Contains(parameters.Q));
            }

            if (!string.IsNullOrWhiteSpace(parameters.OrderBy))
            {
                if (parameters.OrderBy.ToLower() == "name")
                {
                    item = item.OrderBy(x => x.FirstName).ThenBy(x => x.LastName);
                }
            }


            return await PageList<Employee>.CreateAsync(item, parameters.PageNumber, parameters.PageSize);
        }

        public async Task<Employee> GetEmployeeAsync(Guid companyId, Guid employeeId)
        {
            if (companyId.Equals(Guid.Empty))
            {
                throw new ArgumentNullException(nameof(companyId));
            }
            if (employeeId.Equals(Guid.Empty))
            {
                throw new ArgumentNullException(nameof(employeeId));
            }

            return await _context.Employees
                .FirstOrDefaultAsync(et => et.CompanyId == companyId && et.Id == employeeId);
        }

        public void AddEmployee(Guid companyId, Employee employee)
        {
            if (companyId.Equals(Guid.Empty))
            {
                throw new ArgumentNullException(nameof(companyId));
            }
            if (employee == null)
            {
                throw new ArgumentNullException(nameof(employee));
            }

            employee.CompanyId = companyId;
            _context.Employees.Add(employee);
            }

        public void UpdateEmployee(Employee employee)
        {
            _context.SaveChangesAsync();
        }

        public void DeleteEmployee( Employee employee)
        {
            _context.Employees.Remove(employee);
        }

        public void DeleteEmployees(params Employee[] employees)
        {
            _context.Employees.RemoveRange(employees);
        }

        public void SaveAsync()
        {
            _context.SaveChangesAsync();
        }
    }
}